<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:商品库存
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class StocksviewAction extends CommonAction {
       	function _filter(&$map){
        //$map['id'] = array('egt',2);
		$map['partnumber|prodname|partmodel|prodsize'] = array('like',"%".$_POST['selectname']."%");
	}
    
    
    function  exportExcel(){
        /*先查出数据,后引入PHPExcel可以避免ThinkPHP和PHPExcel冲突 HongPing**/
        $stockView = D('Stocksview');
        $stockList = $stockView->select();
        
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new PHPExcel();
        
        $excelTitle = "仓库库存报表";
        $objPHPExcel->getActiveSheet()->mergeCells('A1:L1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)  
            ->setCellValue('A1', $excelTitle.'  导出时间：'.date('Y-m-d H:i:s'))  
            ->setCellValue('A2', '编号')  
            ->setCellValue('B2', '商品编号')  
            ->setCellValue('C2', '商品名称')  
            ->setCellValue('D2', '型号')  
            ->setCellValue('E2', '规格')  
            ->setCellValue('F2', '单位')  
            ->setCellValue('G2', '库存数量')  
            ->setCellValue('H2', '单价(￥)')  
            ->setCellValue('I2', '库存金额(￥)')  
            ->setCellValue('J2', '存放仓库')
            ->setCellValue('K2', '库存更新时间')
            ->setCellValue('L2', '备注');  
          // Miscellaneous glyphs, UTF-8   
        for($i=0;$i<count($stockList);$i++){  
            $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $stockList[$i]['id']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $stockList[$i]['partnumber']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $stockList[$i]['prodname']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), $stockList[$i]['partmodel']);             
            $objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3), $stockList[$i]['prodsize']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+3), $stockList[$i]['unit']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('G'.($i+3), $stockList[$i]['stocknums']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('H'.($i+3), $stockList[$i]['presale']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('I'.($i+3), $stockList[$i]['stockaccount']);  
            $objPHPExcel->getActiveSheet(0)->setCellValue('J'.($i+3), $stockList[$i]['wahname']); 
            $objPHPExcel->getActiveSheet(0)->setCellValue('K'.($i+3), toDate($stockList[$i]['updatetime']));
            $objPHPExcel->getActiveSheet(0)->setCellValue('L'.($i+3), $stockList[$i]['remarks']); 
        }  
  
        header('pragma:public');
        header('X-UA-Compatible:IE=7');      
        header('Content-type:application/vnd.ms-excel;charset=gbk;name="'.$excelTitle.'.xls"');
        header("Content-Disposition:inline;filename=$excelTitle.xls");  
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save('php://output'); 
        exit;        
    }
 
}
?>